package com.example.zhc36.fragment.frag;


import android.annotation.SuppressLint;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.zhc36.fragment.R;
import com.example.zhc36.fragment.apadter.Paadapter;
import com.example.zhc36.fragment.apadter.Rvmsgadapter;
import com.example.zhc36.fragment.bean.javabean;
import com.example.zhc36.fragment.bean.userbean;
import com.example.zhc36.fragment.dao.mainmsgDao;
import com.example.zhc36.fragment.utils.JsonUtils;
import com.example.zhc36.fragment.utils.app.BaseFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


import java.util.List;

import io.realm.RealmList;


public class MainMsgFragment extends BaseFragment {
    Handler handler = new Handler();
    private ViewPager vp;
    private LinearLayout dot;
    RecyclerView rlv;




    private int[] img = {R.drawable.pag1, R.drawable.pag2, R.drawable.pag3, R.drawable.pag4, R.drawable.pag5};
    private ImageView[] dot_img = new ImageView[img.length];

    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        View view = inflater.inflate(R.layout.main_msg_fragment, container, false);
        vp = view.findViewById(R.id.vp);
        dot = view.findViewById(R.id.dot);

        rlv=view.findViewById(R.id.rlv);
        Paadapter paadapter = new Paadapter(img);
        vp.setAdapter(paadapter);
        int i = paadapter.getCount() / 2;
        int zhong = i - i % 5;
        vp.setCurrentItem(zhong, false);

        return view;
    }
    @Override
    public void onStart() {
        handler.postDelayed(r, 2000);
        super.onStart();
    }

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            vp.setCurrentItem(vp.getCurrentItem() + 1);
            handler.postDelayed(r, 2000);
        }
    };

    @Override
    public void onStop() {
        handler.removeCallbacks(r);
        super.onStop();
    }

    @Override
    public void onDestroy() {//销毁线程
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void initData() {
        for (int i = 0; i < img.length; i++) {
            //创建小圆点
            dot_img[i] = new ImageView(getContext());
            dot_img[i].setImageResource(R.drawable.select_dot);
            dot_img[i].setPadding(20, 20, 20, 20);
            LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(50, 50);
            dot.addView(dot_img[i], layout);
        }

        chnangeDot(0);
        String json="[{\"id\":1,\"name\":\"好友\",\"friends\":[{\"id\":1,\"name\":\"红猪\",\"headUrl\":\"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d3198678185,878755003\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":2,\"name\":\"黑帽子\",\"headUrl\":\"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d1934952861,3453579486\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":3,\"name\":\"Ara\",\"headUrl\":\"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d3303741086,3211617265\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":4,\"name\":\"Bri\",\"headUrl\":\"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d4233882545,270382672\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"}]},{\"id\":2,\"name\":\"家人\",\"friends\":[{\"id\":5,\"name\":\"苹果\",\"headUrl\":\"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d2659519208,2040552462\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":6,\"name\":\"Smoke\",\"headUrl\":\"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d3063085903,3230502727\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":7,\"name\":\"BigDog\",\"headUrl\":\"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d2370424667,3031767846\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"}]},{\"id\":3,\"name\":\"XX公司\",\"friends\":[{\"id\":8,\"name\":\"小蜘蛛\",\"headUrl\":\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d1053771801,4239224829\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":9,\"name\":\"Ali\",\"headUrl\":\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d3266183129,26862261\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":10,\"name\":\"Logan\",\"headUrl\":\"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d2503440006,744810055\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":11,\"name\":\"Es\",\"headUrl\":\"http://img4.imgtn.bdimg.com/it/u\\u003d2838532421,4231779873\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"},{\"id\":12,\"name\":\"SmallCat\",\"headUrl\":\"http://img0.imgtn.bdimg.com/it/u\\u003d2661993305,1199724439\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"}]},{\"id\":4,\"name\":\"客户\",\"friends\":[{\"id\":13,\"name\":\"Mask\",\"headUrl\":\"http://img5.imgtn.bdimg.com/it/u\\u003d1606972337,3987749266\\u0026fm\\u003d200\\u0026gp\\u003d0.jpg\"},{\"id\":14,\"name\":\"SmallCar\",\"headUrl\":\"http://img1.imgtn.bdimg.com/it/u\\u003d2623703544,3858420575\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\"}]},{\"id\":5,\"name\":\"新分组\",\"friends\":[]}]\n" +
                "\n";

        List<javabean<List<userbean>>> list = JsonUtils.toBean(json, JsonUtils.newParamType(List.class,
                JsonUtils.newParamType(javabean.class,
                        JsonUtils.newParamType(List.class, userbean.class))));
        mainmsgDao dao = new mainmsgDao();
        for(int i=0;i<list.size();i++){
        dao.insert(list.get(i).getFriends());
    }
        List<userbean> show = dao.show();
        Rvmsgadapter rvmsgadapter = new Rvmsgadapter(show);

        rlv.setLayoutManager(new LinearLayoutManager(getContext()));
        rlv.setAdapter(rvmsgadapter);



    }

    private void chnangeDot(int position) {
        for (int i = 0; i < dot_img.length; i++) {
            if (i == position) {
                dot_img[i].setEnabled(true);

            } else {
                dot_img[i].setEnabled(false);
            }
        }


    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initListener() {
        vp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        handler.removeCallbacks(r);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        handler.postDelayed(r, 2000);


                }


                return false;
            }
        });
        vp.addOnPageChangeListener(new SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                chnangeDot(position % img.length);
            }
        });

    }
}
